/**
 * 分页条
 * 使用方法：
 * 新建PageToolbar
 * 调用addData添加数据，可用使用循环反复添加，也可以添加数组
 * 调用renderTo，指定分页条的生成位置并加入回调函数，在点击分页条时执行回调函数
 */
var PageToolbar = function($viewSize) {
	this.data  = [];
	this.size  = 0;
	this.viewSize  = $viewSize ? $viewSize : 9;
	this.start = 0;
	this.end   = $viewSize - 1;
	this.point = 0;
	this.callback = function() {};

	/**
	 * 添加数据
	 */
	this.addData = function($data) {
		if ($data.constructor === Array) {
			for (var i=0; i<$data.length; i++) {
				this.addData($data[i]);
			}
		} else {
			if (!$data.id) {
				$data.id = IDGenerator.gen();
			}
			this.data.push($data);
			this.size  = this.data.length;
			this.start = 0;
			this.end   = Math.min(this.viewSize-1, this.size-1);
			this.point = 0;
		}
	}

	/**
	 * 在指定的容器生成
	 */
	this.renderTo = function($dom, $callback) {
		if ($callback) {
			this.callback = $callback;
		}
		this._render($dom);
	}

	/*
	 * 在指定的容器生成
	 */
	this._render = function(dom) {
		var $this = this;
		$(dom).empty();
		for (var i=this.start; i<=this.end; i++) {
			var $btn = $("<a></a>");
			$btn.attr("id", this.data[i].id);
			$btn.addClass("btn");
			$btn.attr("title", "第"+(i+1)+"页");
			$btn.text(i+1);
			$btn.click(function() {
				$(dom).find("a.btn").removeClass("current");
				$(this).addClass("current");
				$this.point = (parseInt($(this).text()) - 1);
				//调用回调函数
				$this.callback($this.data[$this.point]);
			});
			$(dom).append($btn);
		}

		if (this.size > this.viewSize) {
			this._renderNextBtn(dom);
			this._renderPrevBtn(dom);
		}

		$(dom).find("a#" + this.data[this.point].id).click();
	}

	/*
	 * 添加第一页、上一屏按钮
	 */
	this._renderPrevBtn = function(dom) {
		var $this = this;
		if (this.start > 0) {
			$(dom).find("a.btn:eq(0)").remove();
			$(dom).find("a.btn:eq(0)").remove();
			
			//上一屏
			var prevBtn = $("<a class='btn prev' title='上一屏'>&lt;</a>");
			prevBtn.click(function() {
				$this._previous();
				$this._render(dom);
			});
			$(dom).prepend(prevBtn);
			
			//第一页
			var firstBtn = $("<a class='btn first' title='第一页'>&lt;&lt;</a>");
			firstBtn.click(function() {
				$this._first();
				$this._render(dom);
			});
			$(dom).prepend(firstBtn);
		}
	}

	/*
	 * 添加下一屏、最后一页按钮
	 */
	this._renderNextBtn = function(dom) {
		var $this = this;
		if (this.end < (this.size-1)) {
			$(dom).find("a.btn:eq(" + (this.viewSize-2) + ")").remove();
			$(dom).find("a.btn:eq(" + (this.viewSize-2) + ")").remove();

			//下一屏
			var nextBtn = $("<a class='btn next' title='下一屏'>&gt;</a>");
			nextBtn.click(function() {
				$this._next();
				$this._render(dom);
			});
			$(dom).append(nextBtn);
			
			//最后一页
			var lastBtn = $("<a class='btn last' title='最后一页'>&gt;&gt;</a>");
			lastBtn.click(function() {
				$this._last();
				$this._render(dom);
			});
			$(dom).append(lastBtn);
		}
	}

	/*
	 * 切换到第一页
	 */
	this._first = function() {
		this.start = 0;
		this.point = 0;
		this.end   = this.viewSize - 1;
	}

	/*
	 * 切换到上一屏
	 */
	this._previous = function() {
		this.start = Math.max(0, this.start-this.viewSize+4);
		this.end   = this.start + this.viewSize - 1;
		if (this.point<this.start || this.point>this.end) {
			this.point = this.end;
		}
		if (this.end < this.size) {
			this.point = this.point - 1;
		}
	}

	/*
	 * 切换到下一屏
	 */
	this._next = function() {
		console.log([this.start, this.end, this.point].join());
		this.end   = Math.min(this.size-1, this.end+this.viewSize-4);
		this.start = this.end - this.viewSize + 1;
		if (this.point<this.start || this.point>this.end) {
			this.point = this.start;
		}
		if (this.start > 0) {
			this.point = this.point + 1;
		}
		console.log([this.start, this.end, this.point].join());
	}

	/*
	 * 切换到最后一页
	 */
	this._last = function() {
		this.end   = this.size - 1;
		this.start = this.end  - this.viewSize + 1;
		this.point = this.size - 1;
	}
}